﻿using System;
using System.Collections.Generic;
using System.IO;

namespace KPI
{
	public partial class SiteMaster : System.Web.UI.MasterPage
	{
		public DataAccess.Models.LoginUserInfo TheUser
		{
			get;
			set;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				this.licon.Text = CommonHelper.ReferenceSiteIcon("kpi.ico");
				this.lscript.Text = CommonHelper.ReferenceScript("jquery-1.10.1.min.js") + CommonHelper.ReferenceScript("kpi_common.js") + CommonHelper.ReferenceScript("jquery-migrate-1.2.1.min.js");

				if(TheUser == null)
				{
					TheUser = KPISiteContext.Current.CurrentUser;
				}
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if(TheUser == null)
			{
				TheUser = KPISiteContext.Current.CurrentUser;
			}

			if(TheUser == null)
			{
				lbtnLogout.Visible = false;
				lCurrentUser.Text = string.Empty;

				if(!this.Page.Request.Url.ToString().ToLower().Contains("login.aspx") && !this.Page.Request.Url.ToString().ToLower().Contains("directpage.aspx"))
				{
					Response.Redirect("~/Login.aspx");
				}
			}
			else
			{
				lbtnLogout.Visible = true;
				if(TheUser.RoleName.Equals("unitadministrators") || TheUser.RoleName.Equals("visitors"))
				{
					lbtnLogout.OnClientClick = "logout_click();";
				}
				lCurrentUser.Text = "当前用户: " + TheUser.UserDisplayName;
				lCurrentGroup.Text = string.IsNullOrWhiteSpace(TheUser.DeptName) ? string.Empty : "单位: " + TheUser.DeptName;

				if(TheUser.RoleName.Equals("administrators") || TheUser.RoleName.Equals("supervisitors"))
				{
					hlPwdReset.Visible = true;
				}
			}
		}

		protected void lbtnLogout_Click(object sender, EventArgs e)
		{
			KPISiteContext.Current.CurrentUser = null;

			Response.Redirect("~/Login.aspx");
		}

		//private List<string> GetThemes()
		//{
		//  var result = new List<string>();

		//  DirectoryInfo dInfo = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("App_Themes"));

		//  DirectoryInfo[] dArrInfo = dInfo.GetDirectories();

		//  if(dArrInfo != null)
		//  {
		//    foreach(DirectoryInfo sDirectory in dArrInfo)
		//    {
		//      result.Add(sDirectory.Name);
		//    }
		//  }

		//  return result;
		//}
	}
}
